package com.dzl.userwx.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.userwx.dao.WelfareUserAddressMapper;
import com.dzl.userwx.pojo.WelfareUserAddress;
import com.dzl.userwx.service.WelfareUserAddressService;
import com.dzl.userwx.pojo.dto.WelfareUserAddressDto;
import com.dzl.util.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户地址表(WelfareUserAddress)表服务实现类
 * @author ChenLong
 * @date 2021-06-05 16:26:37
 */
@Service
public class WelfareUserAddressServiceImpl extends BaseServiceImpl<WelfareUserAddress> implements WelfareUserAddressService {

    @Autowired
    private WelfareUserAddressMapper mapper;

    @Override
    public Map<String, Object> updateUserAddress(WelfareUserAddressDto welfareUserAddressDto, Integer userId) {
        Map<String, Object> resMap = new HashMap<>();
        WelfareUserAddress welfareUserAddress = new WelfareUserAddress();
        welfareUserAddress.setUserId(userId);
        welfareUserAddress.setName(welfareUserAddressDto.getName());
        welfareUserAddress.setCreateTime(new Date());
        welfareUserAddress.setStatus(1);
        if (EmptyUtils.isEmpty(welfareUserAddressDto.getPhone())) {
            resMap.put("code", 1001);
            resMap.put("msg", "电话为空");
            return resMap;
        } else {
            welfareUserAddress.setPhone(welfareUserAddressDto.getPhone());
        }
        if (EmptyUtils.isEmpty(welfareUserAddressDto.getProvince())) {
            resMap.put("code", 1002);
            resMap.put("msg", "省份为空");
            return resMap;
        } else {
            welfareUserAddress.setProvince(welfareUserAddressDto.getProvince());
        }
        if (EmptyUtils.isEmpty(welfareUserAddressDto.getAddress())) {
            resMap.put("code", 1003);
            resMap.put("msg", "详细地址为空");
            return resMap;
        } else {
            welfareUserAddress.setAddress(welfareUserAddressDto.getAddress());
        }
        if (EmptyUtils.isNotEmpty(welfareUserAddressDto.getCity())) {
            welfareUserAddress.setCity(welfareUserAddressDto.getCity());
        }
        if (EmptyUtils.isNotEmpty(welfareUserAddressDto.getArea())) {
            welfareUserAddress.setArea(welfareUserAddressDto.getArea());
        }
        // 插入地址信息到用户地址表，保存为默认地址(有则更新，无则插入)
        WelfareUserAddress welfareUserAddressSelect = new WelfareUserAddress();
        welfareUserAddressSelect.setUserId(userId);
        int count = mapper.selectCount(welfareUserAddressSelect);
        if (count > 0) {
            // 有记录就更新
            Example welfareUserAddressExample = new Example(WelfareUserAddress.class);
            welfareUserAddressExample.createCriteria().andEqualTo("userId", userId);
            mapper.updateByExampleSelective(welfareUserAddress, welfareUserAddressExample);
        } else {
            // 没有记录插入用户地址
            mapper.insert(welfareUserAddress);
        }
        resMap.put("code", 1);
        resMap.put("msg", "success");
        return resMap;
    }
}
